package MainThread;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;

import org.json.JSONObject;

import Bean.Msg;
import Utils.SendMsgUtils;
import Windows.FriendTree;
import Windows.iTalk;

public class ClientThread implements Runnable {
	// 该线程负责处理的Socket
	private Socket s;
	// 该现成所处理的Socket所对应的输入流
	BufferedReader br = null;
	// 创建发送聊天窗口
	public static ArrayList<iTalk> italkList = new ArrayList<>();

	private FriendTree friendTree;
	private String mySelf = null;

	public ClientThread(Socket s, String mySelf) throws IOException {
		this.s = s;
		this.mySelf = mySelf;
		br = new BufferedReader(new InputStreamReader(s.getInputStream()));
		friendTree = new FriendTree(s, mySelf);
		friendTree.setVisible(true);
	}

	public void run() {
		try {
			String message = null;
			// 不断读取Socket输入流中的内容，并将这些内容打印输出
			// 客户端接收的所有数据都在这里
			while ((message = br.readLine()) != null) {
				System.out.println(message);
				JSONObject js = new JSONObject(message);
				int state = js.getInt("login_reg_chat_ack");
				String from = null;
				String to = null;
				if (!js.isNull("from")) {
					from = js.getString("from");
				}
				if (!js.isNull("to")) {
					to = js.getString("to");
				}

				switch (state) {
				case MyClient.CHAT:
					if (FriendTree.friendiTalk.containsKey(from)) {
						iTalk italk = FriendTree.friendiTalk.get(from);
						italk.setT_talkContent(mySelf + ":" + js.getString("content") + "\n");
					} else {
						iTalk italk = new iTalk(s, from, js.getString("to"));
						italk.setVisible(true);
						italk.setT_talkContent(from + ":" + js.getString("content") + "\n");
						FriendTree.friendiTalk.put(from, italk);
					}
					break;
				case MyClient.FRIEND:
					if (js.getString("content").equals("over")) {
						friendTree.treeUpdateUI();
					} else {
						friendTree.flushUI(js.getString("content"));
					}
					break;

				case MyClient.UAD:
					if (js.getString("content").equals("line")) {
						friendTree.FriendUAD(true, from, false);
					} else if (js.getString("content").equals("newfriend")) {
						friendTree.FriendUAD(true, from, true);
					} else {
						friendTree.FriendUAD(false, from, false);
					}

					// if(js.getString("content").equals("line")){
					// friendTree.FriendUAD(true, from,false);
					// }else{
					// friendTree.FriendUAD(false, from,false);
					// }
					break;
				case MyClient.ADD:
					if (js.getString("content").equals("1")) {
						// 弹出窗口
						int option = JOptionPane.showConfirmDialog(null, "来自好友" + from + "的添加请求", "同意添加？",
								JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null);
						switch (option) {
						case JOptionPane.YES_NO_OPTION: {
							Msg msg = new Msg();
							msg.MsgInfo(mySelf, from, MyClient.ADD, "3:A");
							SendMsgUtils.sendMsg(s, new JSONObject(msg).toString());
							break;
						}
						case JOptionPane.NO_OPTION:
							Msg msg = new Msg();
							msg.MsgInfo(mySelf, from, MyClient.ADD, "3:R");
							SendMsgUtils.sendMsg(s, new JSONObject(msg).toString());
							break;
						}
					} else if (js.getString("content").equals("2")) {
						// 弹出对话框，提示对方不在线，无法添加
						// 待添加
						JOptionPane.showMessageDialog(null, "对方不在线，无法添加");
					} else if (js.getString("content").equals("3:A")) {
						// 弹出对话框，提示被同意消息，并刷新列表
						// 待添加
						JOptionPane.showMessageDialog(null, "好友添加成功");
					} else if (js.getString("content").equals("3:R")) {
						// 弹出对话框，提示被拒绝消息
						// 待添加
						JOptionPane.showMessageDialog(null, "好友添加被拒绝");
					}
					break;
				default:
					break;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}